<?php
/**
 * Description of CSVLoader
 *
 * @author LuoYingBin
 */
class CSVLoader {
    //put your code here
    private $file;
    private $splitChar=",";
    public function __construct($filePath) {
        $this->file=fopen($filePath, "rb");
        if($this->file===FALSE){
            throw new Exception("file open error");
        }
    }
    
    public function setSplitChar($c=","){
        $this->splitChar=$c;
    }
    
    public function loadAndDo($fun){
        $headLine= trim(fgets($this->file));
        $fields=explode($this->splitChar, $headLine);
        while(($line=fgets($this->file))!==FALSE){
            $line= trim($line);
            if(empty($line)){
                continue;
            }
            $vals= explode($this->splitChar, $line);
            $row= array_combine($fields, $vals);
            $fun($row);
        }
        fclose($this->file);
    }
    
    public function load(){
        $__data=array();
        $this->loadAndDo(function($row) use(&$__data){
            $__data[]=$row;
        });
        return $__data;
    }
}